package com.qifei.xmlg_backend.controller;

import com.qifei.xmlg_backend.DTO.emp.EmpQueryParam;
import com.qifei.xmlg_backend.entity.Emp;
import com.qifei.xmlg_backend.entity.PageBean;
import com.qifei.xmlg_backend.entity.Result;
import com.qifei.xmlg_backend.service.IEmpService;
import com.qifei.xmlg_backend.service.IStatisticsService;
import io.jsonwebtoken.io.IOException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cglib.core.Local;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 员工
 *
 * @author qifei
 * @since 2025-06-10
 */
@RestController
@RequestMapping("/emps")
@Api(tags = "员工管理接口")
public class EmpController {

    @Autowired
    private IEmpService empService;

    @Autowired
    private IStatisticsService statisticsService;

    /**
     * 查看员工列表（分页）*/
    @GetMapping()
    public Result<?> getEmpList(@ModelAttribute EmpQueryParam param,    @RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pageSize) {
        param.setPage(page);
        param.setPageSize(pageSize);
        PageBean<Emp> pageBean = empService.getEmpList(param);
        return Result.success(pageBean);
    }
    /**
     * 通过id查询
     * @param id
     */
    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable int id   ) {
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }
    /**
     * 添加员工
     *
     */
    @PostMapping()
    public Result<?> add(@RequestBody Emp emp) {

        boolean success = empService.save(emp);
        if (!success){
            return Result.error("添加员工失败");
        }
        return Result.success("添加员工成功");
    }

    /**
     * 更新员工
     *
     */
    @PutMapping()
    public Result<?> update(@RequestBody Emp emp) {
        boolean success = empService.updateById(emp);
        if (!success){
            return Result.error("更新员工信息失败");
        }
        return Result.success("更新员工信息成功");
    }

    /**
     * 删除员工
     *
     */
    @DeleteMapping("/{ids}")
    public Result<?> delete(@PathVariable String ids) {
        String[] idArray = ids.split(",");
        if (idArray.length == 1) {
            // 单个删除
            boolean removed = empService.removeById(Long.valueOf(idArray[0]));
            return removed ? Result.success("删除成功") : Result.error("删除失败");
        }
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::valueOf).toList();
        boolean removed = empService.removeByIds(idList);
        if (removed) {
            return Result.success("批量删除成功");
        } else {
            return Result.error("批量删除失败");
        }

    }
    /**
     * 员工导入
     * */
    @PostMapping("/import")
    public Result<?> importExcel(@RequestParam("file") MultipartFile file) {
        empService.importExcel(file);
        return Result.success("导入成功");
    }

    /**
     * 员工导出
     * */
    @PostMapping("/export")
    public void exportSelected(@RequestBody List<Integer> ids, HttpServletResponse response) throws IOException {
        empService.exportSelected(ids, response);
    }

    /**
     * 获取员工性别统计数据
     */
    @GetMapping("/gender/stats")
    @ApiOperation(value = "获取员工性别统计数据")
    public Result<?> getEmpGenderStats() {
        return Result.success(statisticsService.getEmpGenderStats());
    }

    /**
     * 获取员工部门分布统计数据
     */
    @GetMapping("/dept/stats")
    @ApiOperation(value = "获取员工部门分布统计数据")
    public Result<?> getEmpDeptStats() {
        return Result.success(statisticsService.getEmpDeptStats());
    }

    /**
     * 获取员工入职时间统计数据（按月份）
     */
    @GetMapping("/entrydate/stats")
    @ApiOperation(value = "获取员工入职时间统计数据（按月份）")
    public Result<?> getEmpEntryDateStats() {
        return Result.success(statisticsService.getEmpEntryDateStats());
    }
}
